package com.medica.ledproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medica.ledproject.common.BxyUtil;
import com.medica.ledproject.common.CustomException;
import com.medica.ledproject.entity.*;
import com.medica.ledproject.mapper.DzParkLedscreeMapper;
import com.medica.ledproject.service.IDzParkLedscreeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static java.util.stream.Collectors.toList;

/**
 * <p>
 * LED屏幕 服务实现类
 * </p>
 *
 * @author xiaona
 * @since 2024-05-29
 */
@Service
public class DzParkLedscreeServiceImpl extends ServiceImpl<DzParkLedscreeMapper, DzParkLedscree> implements IDzParkLedscreeService {
    @Autowired
    DzParkLedscreeMapper dzParkLedscreeMapper;
    @Value("${filePath}")
    public  String filePath;
    @Override
    public IPage<DzParkLedscreeRespon> getLedList(int page,int pageSize) {
        IPage<DzParkLedscreeRespon> pages = new Page<>(page, pageSize);
        IPage<DzParkLedscreeRespon> plist=dzParkLedscreeMapper.getledScreeList(pages);
        List<DzParkLedscreeRespon> list =plist.getRecords();
        if(list!=null&&list.size()>0){
            for (DzParkLedscree dz:list){
                if(StringUtils.isNotBlank(dz.getImage())) {
                    dz.setImage(filePath + dz.getImage());
                }
            }
        }
        plist.setRecords(list);
        return plist;
    }

    @Override
    public String updateLegState(RequestPro requestPro,boolean isOpen) {
        try {
            if(requestPro.isAll()){
                //查询列表并循环列表调用静态方法打开或者关闭屏幕，并更新屏幕状态
              List<DzParkLedscree> dzList = dzParkLedscreeMapper.selectList(new QueryWrapper<DzParkLedscree>().eq("is_del","0"));
              if(dzList!=null&&dzList.size()>0){
                  for(DzParkLedscree dz:dzList){
                      boolean success = BxyUtil.openOrDown(dz.getIp(), isOpen);
                      if (success) {
                          dz.setStatus(isOpen ? 1 : 2);
                          dzParkLedscreeMapper.updateById(dz);
                          return "操作成功";
                      }
                  }
              }
            }else {
                //获取所有LED屏幕的地址 判断是否为空
                String[] array = requestPro.getAdress1();
                if (array != null && array.length > 0) {
                    List<DzParkLedscree>   dzList = dzParkLedscreeMapper.selectList(new QueryWrapper<DzParkLedscree>().eq("is_del","0").in("id",array));

                    //循环列表调用静态方法打开或者关闭屏幕，并更新屏幕状态
                    for (DzParkLedscree dzParkLedscree : dzList) {
                        boolean success = BxyUtil.openOrDown(dzParkLedscree.getIp(), isOpen);
                        if (success) {
                            dzParkLedscree.setStatus(isOpen ? 1 : 2);
                            dzParkLedscreeMapper.updateById(dzParkLedscree);
                            return "操作成功";
                        }
                    }
                } else {
                    throw new CustomException("地址参数不能为空");
                }
            }
        }catch (Exception e) {
            throw new CustomException(e.getMessage());
        }

        return "";
    }

    @Override
    public String uploadLegList(MultipartFile file) {
        //实体类集合
        List<DzParkLedscree> listData = new ArrayList<>();
        try{
            if(file == null){
                throw new CustomException("文件不能为空");
            }
            //根据路径获取这个操作excel的实例
            XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
            XSSFSheet sheet = wb.getSheetAt(0);
            XSSFRow row = null;
            //循环sesheet页中数据从第二行开始，第一行是标题
            for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {

                //获取每一行数据
                row = sheet.getRow(i);
                DzParkLedscree data = new DzParkLedscree();
                if(row.getCell(0)!=null) {
                    row.getCell(0).setCellType(CellType.STRING);
                    row.getCell(1).setCellType(CellType.STRING);
                    row.getCell(2).setCellType(CellType.STRING);
                    data.setName(row.getCell(0).getStringCellValue());
                    data.setUniqCode(row.getCell(1).getStringCellValue());
                    data.setIp(row.getCell(2).getStringCellValue());
                    data.setCreatetime(LocalDateTime.now());
                }
                listData.add(data);
            }
        }catch (Exception e){
            throw new CustomException(e.getMessage());
        }

        return this.saveBatch(listData)?"上传成功":"上传失败";
    }

    @Override
    public String updateLedScree(int id, DzParkLedscree reDzParkLedscree) {
        DzParkLedscree dzParkLedscree=dzParkLedscreeMapper.selectById(id);
        if(dzParkLedscree!=null){
            reDzParkLedscree.setId(id);
            return  dzParkLedscreeMapper.updateById(reDzParkLedscree)>0?"操作成功":"操作失败";
        }else{
            throw  new CustomException("没有找到该LED屏幕");
        }
    }
}
